/*
 * @Author: 刘思思
 * @Date: 2024-12-03 17:22:53
 * @LastEditors: 刘思思
 * @LastEditTime: 2024-12-03 17:23:03
 * @FilePath: /AdvertisingMachine/common/md5/src/md5Utils.c
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
#include "md5Utils.h"
FileContent *readFile(char *filePath)
{
    // 声明Filecontent
    FileContent *result = calloc(1, sizeof(FileContent));
    g_print("tiaoshi\n");
    // 打开文件
    FILE *file = fopen(filePath, "rb");
    g_print("tiaoshi\n");
    // 获取文件大小
    fseek(file, 0, SEEK_END);
    int length = ftell(file);
    result->length = length;
    fseek(file, 0, SEEK_SET);
    g_print("jjjjjj\n");
    // 读取文件内容
    result->content = calloc(1, length);
    fread(result->content, sizeof(char), length, file);
    g_print("jjjjjj\n");
    fclose(file);
    // 返回FIlecontent
    return result;
}

// 计算md5
char *getMd5(char *filepath)
{
    char *md5 = NULL;
    // 读取文件到缓冲区
    FileContent *fc = readFile(filepath);
    if (fc == NULL)
    {
        g_warning("ReadFile %s error", filepath);
        return NULL;
    }
    // 调用glib计算md5
    GChecksum *checksum = g_checksum_new(G_CHECKSUM_MD5);
    g_checksum_update(checksum, fc->content, fc->length);
    free(fc->content);
    free(fc);
    md5 = g_checksum_get_string(checksum);
    return md5;
}